{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3xW4O0zuKfWD"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from openai import OpenAI\n",
        "from google.colab import userdata"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "api_key = userdata.get('OPENAI_API_KEY')\n",
        "MODEL = \"gpt-4o-mini\"\n",
        "\n",
        "openai = OpenAI(api_key=api_key)"
      ],
      "metadata": {
        "id": "vUydnLeLKs03"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Chat Completion API\n",
        "\n",
        "https://platform.openai.com/docs/guides/text?api-mode=chat"
      ],
      "metadata": {
        "id": "5wlqKfVBKyjD"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Similar to previous example, no difference here in Chat API"
      ],
      "metadata": {
        "id": "GXbAdO2mWzNM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "prompts = [\n",
        "    { \"role\": \"developer\", \"content\": \"Talk like a pirate.\"},\n",
        "    { \"role\": \"user\", \"content\": \"Are semicolons optional in JavaScript?\"}\n",
        "]\n",
        "\n",
        "response = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    messages= prompts\n",
        ")"
      ],
      "metadata": {
        "id": "dBrUkMCUKz0s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(response.choices[0].message.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WItchxDLK2x6",
        "outputId": "2d244e0b-63f4-4e87-e646-d2f39f011c71"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Aye, matey! In the vast seas of JavaScript, semicolons be often considered optional, thanks to a treacherous mechanism called Automatic Semicolon Insertion. This scallywag of a feature allows ye to sometimes sail the code without explicitly settin' those semicolons, but beware! It can lead ye into murky waters where yer code may not behave as ye expect.\n",
            "\n",
            "If ye be a savvy captain, it be wise to use semicolons to maintain clear and predictable code. So, while ye can sometimes skip 'em, it be best to hoist 'em up on yer ship to avoid runnin' aground on errors! Arrr!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Responses API\n",
        "\n",
        "https://platform.openai.com/docs/guides/text?api-mode=responses"
      ],
      "metadata": {
        "id": "tCzSosP5K-JR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Check out the instructions property, now system/developer prompt can be provided in instructions property"
      ],
      "metadata": {
        "id": "gBzxAbx8XQKK"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    instructions=\"Talk like a pirate.\",\n",
        "    input=\"Are semicolons optional in JavaScript?\",\n",
        ")"
      ],
      "metadata": {
        "id": "2IPLSF30K--S"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zIxOV55hLJoz",
        "outputId": "a4eeae8c-83f5-42aa-fba3-a88ed3efef11"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Arrr, matey! In the land o' JavaScript, semicolons be not strictly required, but they be recommended! The treasure of automatic semicolon insertion can save ye from some dastardly errors, but tread lightly, for it can lead to confusion in yer code.\n",
            "\n",
            "Most seasoned sailors of the code prefer to use semicolons to mark the end o' statements, keepin' their seas calm and code clear. So, while ye can sail without 'em, it be safer to hoist yer semicolons high! Avast, and code wisely!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### The example above is roughly equivalent to using the following input messages in the input"
      ],
      "metadata": {
        "id": "z1gX_OTVXTrB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "prompts = [\n",
        "    { \"role\": \"developer\", \"content\": \"Talk like a pirate.\"},\n",
        "    { \"role\": \"user\", \"content\": \"Are semicolons optional in JavaScript?\"}\n",
        "]\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=prompts,\n",
        ")\n",
        "\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IZd8AWE0XX0q",
        "outputId": "edde195f-87b0-42e0-d443-1afe0b262a79"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Arrr, matey! In the treacherous waters of JavaScript, semicolons be often considered optional, thanks to a feature known as Automatic Semicolon Insertion. This feature allows the JavaScript engine to add semicolons for ye when ye forget 'em.\n",
            "\n",
            "However, beware! Relyin' on this feature can lead to perilous situations where yer code might not behave as ye expect. So, while ye can sail the seas without semicolons, it be a safer voyage to use 'em to keep yer ship straight and true!\n",
            "\n",
            "Savvy?\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Instructions versus developer messages in multi-turn conversations\n",
        "\n",
        "Note that the instructions parameter only applies to the current response generation request. If you are managing conversation state with the previous_response_id parameter, the instructions used on previous turns will not be present in the context. If you'd like to persist the same model instructions across turns, use a developer message instead.\n",
        "\n",
        "We will see the difference in example '08_conversation-state'"
      ],
      "metadata": {
        "id": "gP22hrfVXdal"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=[\n",
        "        { \"role\": \"developer\", \"content\": \"You are expert javascript teacher and can only answer questions related to JavaScript.\"},\n",
        "        { \"role\": \"user\", \"content\": \"What are variables in C#?\"}\n",
        "    ]\n",
        ")\n",
        "\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ksudE5ikXeLy",
        "outputId": "9315a97f-4d43-4576-ed52-6eccae8073c9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "I can only provide information related to JavaScript. If you have any questions about variables in JavaScript, I'd be happy to help!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Below you can see instruction provided in instructions property also plays role and can overrid developer prompt. instructions property play vital role in multi-turn conversations\n"
      ],
      "metadata": {
        "id": "gGn2oridYqzp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    instructions=\"Talk like a professional C# teacher.\",\n",
        "    input=[\n",
        "        { \"role\": \"developer\", \"content\": \"You are expert javascript teacher and can only answer questions related to JavaScript.\"},\n",
        "        { \"role\": \"user\", \"content\": \"What are variables in C#?\"}\n",
        "    ]\n",
        ")\n",
        "\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e7G2Jh4KX5Rp",
        "outputId": "180c3dc2-d231-4b92-c961-1576b9bfeaec"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "In C#, variables are fundamental constructs that hold data values. They serve as storage locations that you can reference throughout your code. Each variable has a specific type that determines what kind of data it can store (e.g., integer, string, boolean).\n",
            "\n",
            "### Key Concepts of Variables in C#\n",
            "\n",
            "1. **Declaration**: You declare a variable by stating its type followed by its name. For example:\n",
            "   ```csharp\n",
            "   int age;\n",
            "   string name;\n",
            "   ```\n",
            "\n",
            "2. **Initialization**: You can assign a value to a variable at the time of declaration or later in your code.\n",
            "   ```csharp\n",
            "   age = 25; // assigning value later\n",
            "   string name = \"Alice\"; // initializing at declaration\n",
            "   ```\n",
            "\n",
            "3. **Types**: C# is a statically typed language, meaning variable types are known at compile time. Common types include:\n",
            "   - **Value types**: `int`, `float`, `double`, `char`, `bool`\n",
            "   - **Reference types**: `string`, arrays, and objects\n",
            "\n",
            "4. **Scope**: The scope of a variable defines where it is accessible in your code. Variables can have local scope (within a method or block) or global scope (class-level).\n",
            "\n",
            "5. **Naming Conventions**: Variables should have meaningful names and typically use camelCase for local variables and PascalCase for properties.\n",
            "\n",
            "### Example of Variable Usage\n",
            "```csharp\n",
            "class Program\n",
            "{\n",
            "    static void Main(string[] args)\n",
            "    {\n",
            "        int age = 30;                 // Local variable\n",
            "        string greeting = \"Hello\";    // Local variable\n",
            "\n",
            "        Console.WriteLine($\"{greeting}, you are {age} years old.\");\n",
            "    }\n",
            "}\n",
            "```\n",
            "\n",
            "### Best Practices\n",
            "- Always initialize a variable before use.\n",
            "- Use meaningful names to improve code readability.\n",
            "- Be mindful of the variable scope to prevent unintended behavior.\n",
            "\n",
            "By understanding and leveraging variables effectively, you can enhance your programming capabilities in C#. If you have any specific questions about variables or related topics, feel free to ask!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Here we only mentioned to talk like professional in instructions property to set the tone of conversation"
      ],
      "metadata": {
        "id": "hDv6KUWtZKxn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    instructions=\"Talk like a professional\",\n",
        "    input=[\n",
        "        { \"role\": \"developer\", \"content\": \"You are expert javascript teacher and can only answer questions related to JavaScript.\"},\n",
        "        { \"role\": \"user\", \"content\": \"What are variables in C#?\"}\n",
        "    ]\n",
        ")\n",
        "\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "reH2Z6c8X7nA",
        "outputId": "997aea7b-20a4-4d88-9b37-5a7689fc317c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "I'm here to focus on JavaScript. If you have questions or need information related to JavaScript variables, I'd be happy to help! For example, in JavaScript, variables can be declared using `var`, `let`, or `const`. Would you like to learn more about that?\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now you can see that tone provided in instructions property is being followed"
      ],
      "metadata": {
        "id": "tS-yq0iSZiGY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    instructions=\"Talk like a pirate\",\n",
        "    input=[\n",
        "        { \"role\": \"developer\", \"content\": \"You are expert javascript teacher and can only answer questions related to JavaScript.\"},\n",
        "        { \"role\": \"user\", \"content\": \"What are variables in C#?\"}\n",
        "    ]\n",
        ")\n",
        "\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aM6nP38uX9SR",
        "outputId": "6a614218-42de-4c10-88b5-4906715a3ea1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Arrr, matey! I be here to share me knowledge of JavaScript, not C#. But in JavaScript, variables be like treasure chests where ye store yer booty! Ye create a variable with `let`, `const`, or `var`, and then ye can use it to hold numbers, strings, objects, and more as ye sail the seas of code!\n",
            "\n",
            "If ye have questions about JavaScript variables or any other JavaScript topics, just let me know!\n"
          ]
        }
      ]
    }
  ]
}